﻿using System.Modules.Constants;
using System.Modules.SharePoint.Contracts.Enums;
using System.Text;
using Microsoft.SharePoint;

namespace System.Modules.SharePoint.Contracts.DTO
{
    /// <summary>
    /// Creates an SP Query Condition
    /// </summary>
    public class SPQueryOperation : SPQueryCondition
    {
        #region Properties
        /// <summary>
        /// Field Ref
        /// </summary>
        public SPQueryFieldRef FieldRef { get; set; }

        /// <summary>
        /// Field Value
        /// </summary>
        public SPQueryValue Value { get; set; }

        /// <summary>
        /// SP Caml Operation
        /// </summary>
        public SPQueryCamlOperation Operation { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Default Constructor
        /// </summary>
        public SPQueryOperation() {}

        /// <summary>
        /// Initializes a new instance of the SPQueryBuilder class.
        /// </summary>
        /// <param name="fieldName">The field name</param>
        /// <param name="fieldValue">The field value</param>
        /// <param name="fieldType">The field type</param>
        public SPQueryOperation(string fieldName, string fieldValue, SPFieldType fieldType)
        {
            FieldRef.FieldName = fieldName;
            Operation = SPQueryCamlOperation.Eq;
            Value.FieldType = fieldType;
            Value.Value = fieldValue;
        }

        /// <summary>
        /// Initializes a new instance of the SPQueryBuilder class.
        /// </summary>
        /// <param name="fieldName">The field name</param>
        /// <param name="fieldValue">The field value</param>
        /// <param name="fieldType">The field type</param>
        /// <param name="operation">The operation used</param>
        public SPQueryOperation(string fieldName, string fieldValue, SPFieldType fieldType, SPQueryCamlOperation operation)
        {
            FieldRef.FieldName = fieldName;
            Operation = operation;
            Value.FieldType = fieldType;
            Value.Value = fieldValue;
        }

        /// <summary>
        /// Initializes a new instance of the SPQueryBuilder class.
        /// </summary>
        /// <param name="fieldName">The field name</param>
        /// <param name="fieldValue">The field value</param>
        /// <param name="fieldType">The field type</param>
        /// <param name="operation">The operation used</param>
        /// <param name="isLookupId"> </param>
        public SPQueryOperation(string fieldName, string fieldValue, SPFieldType fieldType, SPQueryCamlOperation operation, bool isLookupId)
        {
            FieldRef.FieldName = fieldName;
            FieldRef.IsLookupId = isLookupId;
            Operation = operation;
            Value.FieldType = fieldType;
            Value.Value = fieldValue;
        }

        /// <summary>
        /// Constructor Passing Field Ref | Value | Operation
        /// </summary>
        /// <param name="fieldRef"></param>
        /// <param name="value"></param>
        /// <param name="operation"></param>
        public SPQueryOperation(SPQueryFieldRef fieldRef, SPQueryValue value, SPQueryCamlOperation operation)
        {
            FieldRef = fieldRef;
            Value = value;
            Operation = operation;
        }
        #endregion

        #region Implicit Operators
        /// <summary>
        /// Implicit conversion operator to string
        /// </summary>
        /// <param name="value">The SP Query Value</param>
        /// <returns>A string representation of the SP Query Value object</returns>
        public static implicit operator string(SPQueryOperation value)
        {
            return value.ToString();
        }
        #endregion

        #region Override
        /// <summary>
        /// Overrides To String
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var condition = new StringBuilder();
            condition.AppendFormat(XmlTemplates.OPEN_TAG, Operation);
            condition.Append(FieldRef);
            if (Operation != SPQueryCamlOperation.IsNull && Operation != SPQueryCamlOperation.IsNotNull)
            {
                condition.Append(Value);
            }
            condition.AppendFormat(XmlTemplates.CLOSING_TAG, Operation);
            return condition.ToString();
        }
        #endregion
    }

    /// <summary>
    /// SPQueryIsNullOperation
    /// </summary>
    public class SPQueryIsNullOperation : SPQueryOperation
    {
        /// <summary>
        /// Constructor Passing Field Name 
        /// </summary>
        /// <param name="fieldName"></param>
        public SPQueryIsNullOperation (string fieldName)
        {
            FieldRef.FieldName = fieldName;
            Operation = SPQueryCamlOperation.IsNull;
        }

        /// <summary>
        /// Constructor Passing Field Ref 
        /// </summary>
        /// <param name="fieldRef"></param>
        public SPQueryIsNullOperation(SPQueryFieldRef fieldRef)
        {
            FieldRef = fieldRef;
            Operation = SPQueryCamlOperation.IsNull;
        }
    }

    /// <summary>
    /// SPQueryIsNotNullOperation
    /// </summary>
    public class SPQueryIsNotNullOperation : SPQueryOperation
    {
        /// <summary>
        /// Constructor Passing Field Name 
        /// </summary>
        /// <param name="fieldName"></param>
        public SPQueryIsNotNullOperation(string fieldName)
        {
            FieldRef.FieldName = fieldName;
            Operation = SPQueryCamlOperation.IsNotNull;
        }

        /// <summary>
        /// Constructor Passing Field Ref 
        /// </summary>
        /// <param name="fieldRef"></param>
        public SPQueryIsNotNullOperation(SPQueryFieldRef fieldRef)
        {
            FieldRef = fieldRef;
            Operation = SPQueryCamlOperation.IsNotNull;
        }
    }
}
